/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.list.storage;

import java.io.File;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.tiberiumlabs.lailaps.settings.Settings;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class HistoryFilenameFilter implements FilenameFilter {

    private SimpleDateFormat dateFormat;
    private long minimumTime;

    public HistoryFilenameFilter() {
        try {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            long currentTime = dateFormat.parse(dateFormat.format(new Date())).getTime();
            long storeHistoryMillis = 1000l * 60l * 60l * 24l * Settings.getApplicationSettings().getStoreHistoryDays();
            minimumTime = currentTime - storeHistoryMillis;
        } catch (Exception ignore) {
        }
    }

    public boolean accept(File dir, String name) {
        try {
            String nameOnly = name.substring(0, 10);
            long time = dateFormat.parse(nameOnly).getTime();
            return time >= minimumTime;
        } catch (Exception ignore) {
        }
        return false;
    }

}
